package com.example.algorithm.sort.shell;

import com.example.algorithm.sort.insert.Insertion;
import com.example.algorithm.sort.merge.Merge;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 * @program: algorithm
 * @description:
 * @packagename: com.example.algorithm.sort.shell
 * @author: Jay
 * @date: 2022/03/26 18:06:03
 **/
public class SortCompare {

    public static void main(String[] args) throws IOException {
        ArrayList<Integer> list = new ArrayList<>();
        BufferedReader reader = new BufferedReader(new InputStreamReader(SortCompare.class.getClassLoader().getResourceAsStream("reverse_arr.txt")));
        String line = null;
        while ((line = reader.readLine())!=null){
            int i = Integer.parseInt(line);
            list.add(i);
        }

        reader.close();
        Integer[] a = new Integer[list.size()];
        list.toArray(a);
//        testInsertion(a);
        //插入排序执行了38173ms
        testShell(a);
        //希尔排序执行了29ms
        testMerge(a);

    }

    public static void testShell(Integer[] a){
        //1.获取执行之前的时间
        long start = System.currentTimeMillis();

        //执行代码
        Shell.sort(a);

        //2.获取执行之后的时间
        long end = System.currentTimeMillis();

        System.out.println("希尔排序执行了"+(end - start)+"ms");
    }

    public static void testInsertion(Integer[] a){
        //1.获取执行之前的时间
        long start = System.currentTimeMillis();

        //执行代码
        Insertion.sort(a);

        //2.获取执行之后的时间
        long end = System.currentTimeMillis();

        System.out.println("插入排序执行了"+(end - start)+"ms");
    }

    public static void testMerge(Integer[] a){
        //1.获取执行之前的时间
        long start = System.currentTimeMillis();

        //执行代码
        Merge.sort(a);

        //2.获取执行之后的时间
        long end = System.currentTimeMillis();

        System.out.println("归并排序执行了"+(end - start)+"ms");
    }
}
